// 本文件对TLV的方法进行性能测试
// @Author: zhengqi Wu
// @Description:
// @Version: 1.0.0
// @Date: 2021/4/8 9:11 下午
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//
const benchmark = require('benchmark')
const TLV = require('./TLV')
var suite = new benchmark.Suite()
var suite2 = new benchmark.Suite()
var suite3 = new benchmark.Suite()
var suite4 = new benchmark.Suite()

let buf = new ArrayBuffer(10)
let view = new DataView(buf)
view.setUint8(0, 240)

let buf2 = new ArrayBuffer(10)
let view2 = new DataView(buf2)
view2.setUint8(0, 241)
view2.setUint16(1, 241, false)

let buf3 = new ArrayBuffer(10)
let view3 = new DataView(buf3)
view3.setUint8(0, 242)
view3.setUint32(1, 4294967295, false)

let buf4 = new ArrayBuffer(10)
let view4 = new DataView(buf4)
view4.setUint8(0, 243)
view4.setBigUint64(1, 18446744073709551615n, false)
var read = function (buf){
    TLV.readVarNumber(buf, 0)
}

// readVarNumber性能测试
suite.add('测试单个字节读取性能', () => {
    read(buf)
}).add('测试两个字节读取性能', () =>{
    read(buf2)
}).add('测试四个字节读取性能', () =>{
    read(buf3)
}).add('测试八个字节读取性能', () =>{
    read(buf4)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('finished')
}).run()

// readType性能测试
view2.setUint16(1, 300, false)
suite2.add('测试合法值1', () => {
    TLV.readType(buf, 0)
}).add('测试合法值2', () =>{
    TLV.readType(buf2, 0)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('readType finished')
}).run()

// readNonNegativeInteger性能测试
buf3 = new ArrayBuffer(100)
view3 = new DataView(buf3)
view3.setUint8(0, 240)
view3.setUint16(1, 4567, false)
view3.setUint32(3, 5678, false)
view3.setBigUint64(7, 78910n, false)
suite3.add('测试合法值1', () => {
    TLV.readNonNegativeInteger(buf3, 1, 3)
}).add('测试合法值2', () =>{
    TLV.readNonNegativeInteger(buf3, 3, 7)
}).add('测试合法值3', () =>{
    TLV.readNonNegativeInteger(buf3, 7, 15)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('readNonNegativeInteger finished')
}).run()

// getSizeOfVarNumber性能测试
suite4.add('测试单个字节读取性能', () => {
    TLV.sizeOfVarNumber(1)
}).add('测试三个字节读取性能', () =>{
    TLV.sizeOfVarNumber(241)
}).add('测试五个字节读取性能', () =>{
    TLV.sizeOfVarNumber(4294967295)
}).add('测试九个字节读取性能', () =>{
    TLV.sizeOfVarNumber(18446744073709551615n)
}).on('cycle', (event) => {
    console.log(String(event.target))
}).on('complete', () =>{
    console.log('sizeOfVarNumber finished')
}).run()